package com.xneure.utils.formatfile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XMLObject{
	Document document = null;// 文档对象
	Element root = null; // 文档根目录
	SAXReader reader = new SAXReader();// 读文档对象
	XMLWriter writer = null;// 声明写XML的对象
	OutputFormat XMLformat = OutputFormat.createPrettyPrint();// XML对齐格式
	String XMLFile = null;

	public XMLObject(){/** 指定XML编码 */
		XMLformat.setEncoding("utf-8");
	}

	public XMLObject(String FileName){
		this();
		ReadFile(FileName);
	}

	public int ReadFile(String fs){// 读取XML文件
		int r = 0;
		File file = null;
		try{
			file = new File(fs);// 文档对象
			if(file.exists() == true){// 文件存在时的操作
				if(ReadData(new InputStreamReader(new FileInputStream(file),"utf-8"))){
					r = 0;
				}else{
					r = -1;
				}
			}else{// 文件不存在时创建一默认对象
				document = DocumentHelper.createDocument();
				r = 1;
			}
			root = document.getRootElement();// 获取根节点
			XMLFile = fs;
		}catch(Exception e){
			e.printStackTrace();
			r = -1;
		}
		return r;
	}

	public boolean ReadData(String xmlStr){
		return ReadData(new StringReader(xmlStr));
	}

	public boolean ReadData(Reader reader){// 读取XML文件
		try{
			document = this.reader.read(reader);
			root = document.getRootElement();// 获取根节点
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	public boolean ReadData(InputStream inputStream){// 读取XML文件
		try{
			document = this.reader.read(inputStream);
			root = document.getRootElement();// 获取根节点
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Element> getElements(String nodePath){
		return root.selectNodes(nodePath);
	}

	public Element getElement(String nodePath){
		return (Element)root.selectSingleNode(nodePath);
	}

	public boolean addNode(String fName,String Name,String[] dataName,String[] data) throws IOException{
		Element el = null;
		if(fName.length() > 0){
			if(root.nodeCount() > 0){
				Element me = null;
				me = root.element(fName);
				el = me.addElement(Name);
			}else{
				el = root.addElement(Name);
			}
		}else{// 对象不存在则新建
			if(root == null){
				el = document.addElement(Name);
			}else{
				el = root.addElement(Name);
			}
			root = document.getRootElement();// 重新获取根目录
		}
		if(null != el){
			for(int i = 0;i <= data.length - 1;i++){// 添加属性
				el.addAttribute(dataName[i],data[i]);
			}
			root = document.getRootElement();
			return true;
		}
		return false;
	}

	public Element getRoot(){
		return root;
	}

	public boolean SaveXML(){
		try{
			writer = new XMLWriter(new FileWriter(XMLFile),XMLformat);
			writer.write(document);
			writer.close();
			return true;
		}catch(IOException e){
			return false;
		}
	}
}
